import logging

from django.contrib import admin

from orders.models import Contract, ContractProduct, Groups, Product, ProductSection, StockRecord, Users


@admin.register(ProductSection)
class ProductSectionAdmin(admin.ModelAdmin):
    list_display = (
        "name",
    )
    readonly_fields = ("create_at", "update_at")
    list_display_links = ("name",)
    search_fields = ("name",)
    list_per_page = 25


@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    list_display = ("name", "product_section", "warehouse", "measure")
    readonly_fields = ("create_at", "update_at")
    list_filter = ("product_section",)
    list_display_links = ("name",)
    search_fields = ("name",)
    list_per_page = 25


@admin.register(Contract)
class ContractAdmin(admin.ModelAdmin):
    list_display = (
        "organization_name",
        "contract_number",
        "start_date",
        "is_closed",
        "is_active",
        )
    readonly_fields=("create_at", "update_at")
    list_display_links = ("contract_number",)
    search_fields = ("organization_name", "contract_number", "product", "is_active")
    list_per_page = 25
    def is_active(self, instance):  # noqa: ANN201
        return instance.is_active
    is_active.boolean = True


@admin.register(ContractProduct)
class ContractProductAdmin(admin.ModelAdmin):
    list_display = (
        "product",
        "contract",
        "oms_balance",
        "ob_balance",
        "pd_balance",
        )
    readonly_fields=("create_at", "update_at")
    list_display_links = ("product",)
    search_fields = ("product__name", "contract")
    list_per_page = 25


@admin.register(StockRecord)
class StockRecordAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "contract_product",
        "document_date",
        "incoming_weight",
        "outgoing_weight",
        "stock_code",
        # "warehouse_balance",
        # "contract_balance",
    )
    readonly_fields = ("create_at", "update_at")
    list_display_links = ("id",)
    search_fields = ("contruct__contract_number", "id")
    list_per_page = 25


# class GroupsInlineAdmin(admin.TabularInline):
#     model = Users.groups.through
#     fields = ("group",)


@admin.register(Users)
class UsersAdmin(admin.ModelAdmin):
    # inlines = (GroupsInlineAdmin,)
    list_display = ("user", "fio_short", "dolzh", "groupa")
    readonly_fields = ("create_at", "update_at")
    list_display_links = ("user",)
    search_fields = ("user__username", "user__first_name")
    list_per_page = 25


@admin.register(Groups)
class GroupsAdmin(admin.ModelAdmin):
    list_display = ("group",)
    list_display_links = ("group",)
    readonly_fields = ("create_at", "update_at")
    search_fields = ("group",)
    list_per_page = 25
